package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 向购物车添加菜品
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 第一步：根据客户端传来的菜品id和套餐id以及通过jwt获取用户id，查询购物车是否有菜品或者套餐存在
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        // 查询
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if (shoppingCartList.size() > 0 && shoppingCartList != null) {
            // 第二步：如果存在，就在原来的基础上数量加一
            ShoppingCart cart = shoppingCartList.get(0);
            // 为购物车从查询的菜品或者套餐取出值
            shoppingCart.setNumber(cart.getNumber() + 1);
            shoppingCart.setId(cart.getId());
            // 更新当前菜品或者套餐的数量
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {
            // 第三步：如果不存在，就插入一条新数据
            if (shoppingCartDTO.getDishId() != null) {
                // 如果是菜品，查询菜品信息，获取菜品名称、单价
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            } else {
                // 如果是套餐，查询套餐信息，获取套餐名称、单价
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            }
        }
    }

    /**
     * 查询购物车（当前登录用户的购物车信息）
     * @return
     */
    public List<ShoppingCart> list() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(currentId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     * @return
     */
    public void clean() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(currentId);
    }

    /**
     * 删除购物车中的菜品
     * @param shoppingCartDTO
     * @return
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 第一步：根据客户端传来的菜品id和套餐id以及通过jwt获取用户id，查询购物车是否有菜品或者套餐存在
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        // 第二步：判断当前菜品或者套餐的数量是否大于1，如果大于1，就在原来的基础上数量减一
        if (shoppingCartList.size() > 0 && shoppingCartList != null) {
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setUserId(userId);
            if (cart.getNumber() == 1) {
                shoppingCartMapper.deleteByCart(cart);
            } else if (cart.getNumber() > 1) {
                shoppingCart.setNumber(cart.getNumber() - 1);
                shoppingCart.setId(cart.getId());
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }
    }
}
